package com.ittony.controller.admin;

import com.ittony.dto.CategoryDTO;
import com.ittony.dto.CategoryPageQueryDTO;
import com.ittony.entity.Category;
import com.ittony.result.PageResult;
import com.ittony.result.Result;
import com.ittony.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "Category Interface")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation("Add Category")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {

        categoryService.addCategory(categoryDTO);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("Category Page")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("Category Page:{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("Delete Category Based on Id")
    public Result deleteCategoryById(@RequestParam Long id) {
        categoryService.deleteCategoryById(id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("Update Category")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("Enable/Disable Category")
    public Result enableDisableCategory(@PathVariable Integer status,Long id) {
        categoryService.enableDisableCategory(status,id);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("Find Category Based on Type")
    public Result listCategoryByType(@RequestParam Integer type) {
        List<Category> categoryList = categoryService.listCategoryByType(type);

        return Result.success(categoryList);
    }
}
